package top.wangxiaomei.ai.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import top.wangxiaomei.ai.domain.Models;
import top.wangxiaomei.ai.other.Abs;
import top.wangxiaomei.ai.other.TreeNode;
import top.wangxiaomei.ai.other.WordEX;
import top.wangxiaomei.ai.service.ModelsService;
import top.wangxiaomei.ai.mapper.ModelsMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/**
* @author Work
* &#064;description  针对表【models】的数据库操作Service实现
* &#064;createDate  2024-07-04 10:00:11
 */
@Service
public class ModelsServiceImpl extends ServiceImpl<ModelsMapper, Models>
    implements ModelsService {
    @Autowired
    public WordsServiceImpl wordsService;
    // 缓存 machModel 的结果
    HashMap<String, List<Abs>> cache = new HashMap<>();


    public String defModel(String word) {
        String result = "未知错误";
        word = word.trim();
        String[] w = word.split(" ");
        if (w.length == 4) {
            Models models = new Models(w[0], w[1], w[2],w[3]);
            result = this.baseMapper.insert(models) > 0 ? "录入成功" : "录入失败";
        }
        return result;
    }

    @Override
    public List<Abs> machModel(WordEX left, WordEX right, TreeNode<WordEX> treeNode) {
        if (true){
            return type1(left, right,treeNode);
        }else {
            return type2(left, right);
        }
    }

    private List<Abs> type2(WordEX left, WordEX right) {
        return null;
    }

    private List<Abs> type1(WordEX left, WordEX right,TreeNode<WordEX> treeNode) {
        if (cache.containsKey(left.getWord() +" "+ right.getWord())) {
            return cache.get(left.getWord() +" "+ right.getWord());
        }
        List<Abs> models = new ArrayList<>();
        for (Abs leftEx: left.getEx()) {
            for (Abs rightEx : right.getEx()) {
                ArrayList<Models> m = this.baseMapper.findModel(leftEx.getName(),rightEx.getName());
                if (m.isEmpty()) continue;

                // 匹配类型处理
                matchTypeProcessing(m,models,treeNode,left,right);

            }
        }
        cache.put(left.getWord() +" "+ right.getWord(), models);
        return models;
    }

    private void matchTypeProcessing(ArrayList<Models> m, List<Abs> models,TreeNode<WordEX> treeNode,WordEX left, WordEX right) {
        for (Models model : m) {
            Abs abs = null;
            if (Objects.equals(model.getModel(), "0")){
                abs = new Abs(model.getMatche1()+model.getMatche2(),Float.parseFloat(model.getModel()));
            }
            if (Objects.equals(model.getModel(), "1")){
                abs = new Abs(model.getData(),Float.parseFloat(model.getModel()));
            }
            if (Objects.equals(model.getModel(), "2")){
                List<Abs> absList = wordsService.getEx(model.getData());
                abs = new Abs(model.getData(),Float.parseFloat(model.getModel()));
                models.add(abs);
                for (Abs abs1 : absList) {
                    abs = new Abs(abs1.getName(),Float.parseFloat(model.getModel()));
                    models.add(abs);
                }
            }
            models.add(abs);
        }
    }
}




